package com.example.demo;

/**
 * @author void
 * @date 2022/2/14 14:28
 * @desc 双重校验锁方式实现单例模式
 */
public class Singleton {
    
    private volatile static Singleton uniqueInstance;
    
    private Singleton (){
        
    }
    
    public static Singleton getUniqueInstance(){
        //对象没有实例化过才进入代码
        if(uniqueInstance==null){
            //类对象加锁
            synchronized (Singleton.class){
                if(uniqueInstance==null){
                    uniqueInstance = new Singleton();
                }
            }
        }
        return uniqueInstance;
    }

    public static void main(String[] args) {
        Singleton singleton = Singleton.getUniqueInstance();
        System.out.println(111);
    }
}
